package routers

import (
	"alertmanagerWebhook/global"
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
)

type RouterGroup struct {
	*gin.RouterGroup
}

func InitRouterGroup() *gin.Engine {
	gin.SetMode(global.Config.System.Env)
	// 完全禁用Gin默认日志输出到控制台
	gin.DefaultWriter = global.Logger.Writer()
	gin.DefaultErrorWriter = global.Logger.Writer()
	router := gin.New()
	// 将 Logger 输出到 logrusg
	router.Use(gin.LoggerWithWriter(global.Logger.Writer()))
	// 恢复中间件，用于处理错误
	router.Use(gin.Recovery())
	// 自定义api 接口调用日志格式只写入logrus
	router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
		// the client access log format
		return fmt.Sprintf("%s - - [%s] \"%s %s %s\" %d %s \"%s\" \"%s\"\n",
			param.ClientIP,
			param.TimeStamp.Format(time.RFC1123),
			param.Method,
			param.Path,
			param.Request.Proto,
			param.StatusCode,
			param.Latency,
			param.Request.UserAgent(),
			param.ErrorMessage,
		)
	}))
	//路由分组
	apiRouterGroup := router.Group("api")
	routerGroup := RouterGroup{apiRouterGroup}
	routerGroup.SendWeChat_Router()
	routerGroup.SendDingDing_alert_Router()
	routerGroup.SendEmail_alert_Router()
	return router
}
